package com.qg.cyapp.activity;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.qg.cyapp.R;
import com.qg.cyapp.adapter.MenuSingleAdapter;
import com.qg.cyapp.bean.FoodBean;
import com.qg.cyapp.bean.Item;
import com.qg.cyapp.component.Dolly;
import com.qg.cyapp.util.MyAlertDialog;
import com.qg.cyapp.util.ShareUtil;

@SuppressLint("NewApi")
public class MenuSingleActivity extends BaseActivity implements
		OnPageChangeListener {
	private List<FoodBean> list;
	private FoodBean food;
	private ViewPager vp;
	private TextView tvBack, tvSureOrder, tvLeftPager, tvRightPager,
			tvFoodName, tvJieSuan;
	private MyAlertDialog dialog;
	private PagerAdapter adapter;
	public static int fooNum = 0;

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_menu_single;
	}

	@Override
	public void setUI() {
		// TODO Auto-generated method stub
		Intent intent = getIntent();
		int activity = intent.getIntExtra(ShareUtil.ACTIVITY_KEY, 0);
		int position = intent.getIntExtra(ShareUtil.POSITION_KEY, 0);
		if (activity == ShareUtil.NormalDishesActivity) {
			list = NormalDishesActivity.foods;
		} else if (activity == ShareUtil.MedicinalDishesActivity) {
			list = MedicinalDishesActivity.foods;
		} else if (activity == ShareUtil.SearchShowActivity) {
			list = SearchShowActivity.foods;
		}
		Log.v(TAG, ShareUtil.gson.toJson(list));

		tvLeftPager = (TextView) mfindViewAndSetOnClick(R.id.tv_previous);
		tvRightPager = (TextView) mfindViewAndSetOnClick(R.id.tv_next);
		tvBack = (TextView) mfindViewAndSetOnClick(R.id.tv_back);
		tvSureOrder = (TextView) mfindViewAndSetOnClick(R.id.tv_sure_order);
		tvFoodName = (TextView) findViewById(R.id.tv_food_name);
		tvJieSuan = (TextView) mfindViewAndSetOnClick(R.id.tv_food_jiesuan);

		vp = (ViewPager) findViewById(R.id.vPager);
		adapter = new MenuSingleAdapter(MenuSingleActivity.this, list);
		vp.setAdapter(adapter);
		vp.setOnPageChangeListener(this);
		if (list.size() != 0) {
			// 当前项的放大坐标
			int currentItem = Integer.MAX_VALUE / 2
					- ((Integer.MAX_VALUE / 2) % list.size()) + position;
			// 实际位于list中的位置
			int curPosition = currentItem % list.size();
			food = list.get(curPosition);
			vp.setCurrentItem(currentItem);
			onPageSelected(currentItem);
		}
	}

	@Override
	public void OnClickListener(View view) {
		switch (view.getId()) {
		case R.id.tv_previous:
			vp.arrowScroll(1);
			break;
		case R.id.tv_next:
			vp.arrowScroll(2);
			break;
		case R.id.tv_back:
			this.finish();
			break;
		case R.id.tv_sure_order:
			addFoodInDolly();
			break;
		case R.id.tv_food_jiesuan:
			Intent i = new Intent(MenuSingleActivity.this,
					ShoppingDollyActivity.class);
			startActivity(i);
			break;
		default:
			break;
		}
	}

	public void addFoodInDolly() {
		final EditText etNum = new EditText(MenuSingleActivity.this);
		etNum.setInputType(InputType.TYPE_CLASS_NUMBER);

		new AlertDialog.Builder(MenuSingleActivity.this)
				.setTitle("请问您需要几份？")
				.setView(etNum)
				.setPositiveButton("确定",
						new android.content.DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								if (!etNum.getText().toString().contains(" ")) {
									fooNum = Integer.parseInt(etNum
											.getText().toString());
									if (fooNum <= 0) {
										Toast.makeText(MenuSingleActivity.this,
												"您输入的数字不合法，请重新输入", 500).show();
									} else {
										Item item = new Item();
										Dolly dolly = Dolly.getDolly();
										dolly.addFood(food, fooNum);
										Toast.makeText(MenuSingleActivity.this,
												"成功添加到购物车，右上方按钮可进行结算", 500)
												.show();
									}
								} else {
									Toast.makeText(MenuSingleActivity.this,
											"您输入的数据不合法，请重新输入", 500).show();
								}
							}
						})
				.setNegativeButton("取消",
						new android.content.DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								dialog.dismiss();
							}
						}).show();

	}

	private View npsetOnValueChangedListener(
			OnValueChangeListener onValueChangeListener) {
		// TODO Auto-generated method stub
		return null;
	}

	MyAlertDialog.MyCallBack callback = new MyAlertDialog.MyCallBack() {

		@Override
		public void doAfter() {
			// TODO Auto-generated method stub
			dialog.show("订餐成功", null, false);
		}

	};

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int position) {
		// TODO Auto-generated method stub
		Log.v(" position : ", " " + position);
		if (list.size() != 0) {
			int curPosition = position % list.size();
			food = list.get(curPosition);
			tvFoodName.setText(food.name);
		} else {
			tvFoodName.setText(food.name);
		}

	}

}
